/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 */

package com.nail.zhaogr.front.action;

import java.util.Hashtable;
import java.util.Map;

import javacommon.util.Md5Bean;
import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import cn.org.rapid_framework.web.scope.Flash;
import cn.org.rapid_framework.web.util.HttpUtils;

import com.nail.zhaogr.model.Members;
import com.nail.zhaogr.service.MembersManager;
import com.nail.zhaogr.vo.query.MembersQuery;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */


public class MembersAction extends BaseAction{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String QUERY_JSP = "/pages/Members/query.jsp";
	protected static final String LIST_JSP= "/pages/Members/list.jsp";
	protected static final String CREATE_JSP = "/pages/Members/create.jsp";
	protected static final String EDIT_JSP = "/front/Members/memberInfo.jsp";
	protected static final String SHOW_JSP = "/pages/Members/show.jsp";
	
	protected static final String ChangePwd_JSP = "/front/Members/changePassword.jsp";
	
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/Members/list.do";
	
	private MembersManager membersManager;
	
	private Members members;
	java.lang.Long id = null;
	private String[] items;
	
	private String newPassword;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			members = new Members();
		} else {
			members = (Members)membersManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性 */
	public void setMembersManager(MembersManager manager) {
		this.membersManager = manager;
	}	
	
	public Object getModel() {
		return members;
	}
	
	public void setMemberId(java.lang.Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/** 进入查询页面 */
	public String query() {
		return QUERY_JSP;
	}
	
	/** 执行搜索 */
	public String list() {
		MembersQuery query = newQuery(MembersQuery.class,DEFAULT_SORT_COLUMNS);
		
		Page page = membersManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	public String save() {
		membersManager.save(members);
		return LIST_ACTION;
	}
	
	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public String update() {
		membersManager.update(this.members);
		return LIST_ACTION;
	}
	
	public String changePwd(){
		members = this.getMemberInfo();
		return ChangePwd_JSP;
	}
	public String savePwd(){
		members.setPassword(Md5Bean.getInstance().getMD5ofStr(members.getPassword()));
		members = membersManager.login(members);
		if(members!=null)
		{
			members.setPassword(Md5Bean.getInstance().getMD5ofStr(newPassword));
			membersManager.update(members);
			Flash.current().success("密码修改成功");
		}else{
			members = this.getMemberInfo();
			Flash.current().error("密码修改失败，原密码错误，请重试！");
		}
		return ChangePwd_JSP;
	}
	/**删除对象*/
	public String delete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			java.lang.Long id = new java.lang.Long((String)params.get("memberId"));
			membersManager.removeById(id);
		}
		return LIST_ACTION;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

}
